package com.blast.sspai.ShareUtil;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/6/16
 * Email: 531057892@qq.com
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.blast.sspai.R;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;

import java.util.HashMap;
import java.util.Map;

/**
 * 分享工具类
 */
public class ShareUtil implements View.OnClickListener {

    private Activity context;

    private String imageUrl;

    private String contentUrl;

    private String title;

    private static ShareUtil shareUtil;

    private UMSocialService mController = UMServiceFactory.getUMSocialService("com.umeng.share");

    Map<String, SHARE_MEDIA> mPlatformsMap = new HashMap<String, SHARE_MEDIA>();
    private AlertDialog dialog;

    private ShareUtil() {
    }

    /**
     * 初始化获取分享对象
     *
     * @return
     */
    public static ShareUtil getShareUtil() {
        ShareUtil ret;
        if (shareUtil == null) {
            ret = new ShareUtil();
        } else {
            ret = shareUtil;
        }
        return ret;
    }

    /**
     * 获取上下文 返回UMSocialService
     *
     * @param context1
     * @return
     */
    public UMSocialService newInstants(Context context1) {
        if (context1 instanceof Activity) {
            context = (Activity) context1;
        } else {
            throw new IllegalArgumentException("请传入activity的上下文！");
        }
        return mController;
    }

    /**
     * 初始化SDK，添加需要的平台
     */
    public void initSocialSdk() {
        //qq平台
        UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(context, "100424468",
                "c7394704798a158208a74ab60104f0ba");
        qqSsoHandler.addToSocialSDK();

        //qq空间
        QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(context,
                "100424468", "c7394704798a158208a74ab60104f0ba");
        qZoneSsoHandler.addToSocialSDK();

        //微信
        String appID = "wx967daebe835fbeac";
        String appSecret = "5fa9e68ca3970e87a1f83e563c8dcbce";
        UMWXHandler umwxHandler = new UMWXHandler(context, appID, appSecret);
        umwxHandler.addToSocialSDK();

        //微信朋友圈
        UMWXHandler wxCircleHandler = new UMWXHandler(context, appID, appSecret);
        wxCircleHandler.setToCircle(true);
        wxCircleHandler.addToSocialSDK();

        mController.getConfig().setSsoHandler(new SinaSsoHandler());

//        mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
    }

    /**
     * 点击分享弹出Dialog,进行分享
     *
     * @param imageUrl   缩小的图片地址
     * @param contentUrl 文章内容地址
     * @param title      分享的标题
     */
    public void showCustomUI(final String imageUrl, final String contentUrl, final String title) {
        if (imageUrl != null && contentUrl != null && title != null) {
            this.imageUrl = imageUrl;
            this.contentUrl = contentUrl;
            this.title = title;
        }
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.share_item, null, false);

        //点击事件
        LinearLayout btnShareQQ = (LinearLayout) view.findViewById(R.id.btn_share_qq);
        btnShareQQ.setOnClickListener(this);
        LinearLayout btnShareQZone = (LinearLayout) view.findViewById(R.id.btn_share_qzone);
        btnShareQZone.setOnClickListener(this);
        LinearLayout btnShareWX = (LinearLayout) view.findViewById(R.id.btn_share_wechat);
        btnShareWX.setOnClickListener(this);
        LinearLayout btnShareWXCircle = (LinearLayout) view.findViewById(R.id.btn_share_wxcircle);
        btnShareWXCircle.setOnClickListener(this);
        LinearLayout btnShareSina = (LinearLayout) view.findViewById(R.id.btn_share_sina);
        btnShareSina.setOnClickListener(this);
//        LinearLayout btnShareTx = (LinearLayout) view.findViewById(R.id.btn_share_tx);
//        btnShareTx.setOnClickListener(this);

        builder.setView(view);
        builder.create();
        dialog = builder.show();
    }

    /**
     * 分享监听器
     */
    SocializeListeners.SnsPostListener mShareListener = new SocializeListeners.SnsPostListener() {

        @Override
        public void onStart() {

        }

        @Override
        public void onComplete(SHARE_MEDIA platform, int stCode,
                               SocializeEntity entity) {
            if (stCode == 200) {

                Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(context,
                        "分享失败 : error code : " + stCode, Toast.LENGTH_SHORT)
                        .show();
            }
        }
    };

    @Override
    public void onClick(View v) {
        int id = v.getId();
        dialog.dismiss();
        switch (id) {
            case R.id.btn_share_qq:
                QQShareContent qqShareContent = new QQShareContent();
                qqShareContent.setTitle(title);
                qqShareContent.setTargetUrl(contentUrl);
                qqShareContent.setShareImage(new UMImage(context, imageUrl));
                qqShareContent.setShareContent(title);
                mController.setShareMedia(qqShareContent);
                mController.postShare(context, SHARE_MEDIA.QQ, mShareListener);
                mController.setAppWebSite(SHARE_MEDIA.QQ,"http://www.umeng.com/social");
                break;
            case R.id.btn_share_qzone:
                QZoneShareContent qZoneShareContent = new QZoneShareContent();
                qZoneShareContent.setTitle(title);
                qZoneShareContent.setShareContent(title);
                qZoneShareContent.setTargetUrl(contentUrl);
                qZoneShareContent.setShareImage(new UMImage(context, imageUrl));
                mController.setShareMedia(qZoneShareContent);
                mController.postShare(context, SHARE_MEDIA.QZONE, mShareListener);
                break;
            case R.id.btn_share_sina:
                SinaShareContent sinaShareContent = new SinaShareContent();
                sinaShareContent.setShareContent(title);
                sinaShareContent.setTargetUrl(contentUrl);
                sinaShareContent.setShareImage(new UMImage(context, imageUrl));
                mController.setShareMedia(sinaShareContent);
                mController.postShare(context, SHARE_MEDIA.SINA, mShareListener);
                break;
//            case R.id.btn_share_tx:
//                TencentWbShareContent tencentWbShareContent = new TencentWbShareContent();
//                tencentWbShareContent.setTitle(title);
//                tencentWbShareContent.setShareContent(title);
//                tencentWbShareContent.setTargetUrl(contentUrl);
//                tencentWbShareContent.setShareImage(new UMImage(context,imageUrl));
//                mController.setShareMedia(tencentWbShareContent);
//                mController.postShare(context,SHARE_MEDIA.TENCENT,mShareListener);
//                break;
            case R.id.btn_share_wechat:
                WeiXinShareContent xinShareContent = new WeiXinShareContent();
                xinShareContent.setTitle(title);
                xinShareContent.setShareContent(title);
                xinShareContent.setTargetUrl(contentUrl);
                xinShareContent.setShareImage(new UMImage(context, imageUrl));
                mController.setShareMedia(xinShareContent);
                mController.postShare(context, SHARE_MEDIA.WEIXIN, mShareListener);
                break;
            case R.id.btn_share_wxcircle:
                CircleShareContent circleShareContent = new CircleShareContent();
                circleShareContent.setTitle(title);
                circleShareContent.setShareContent(title);
                circleShareContent.setTargetUrl(contentUrl);
                circleShareContent.setShareImage(new UMImage(context, imageUrl));
                mController.setShareMedia(circleShareContent);
                mController.postShare(context, SHARE_MEDIA.WEIXIN_CIRCLE, mShareListener);
                break;

        }
    }
}
